# -* - coding: UTF-8 -* -
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')

import threading,string,subprocess
from time import sleep
from task import *
import logging
#self.coop,self.service_center,self.server)
#从外部队列读取消息放入内部对列
class ServerCommander():  
	def __init__(self,coop):  
		self.coop = coop
		self.sql_center=self.coop.sql_center
		self.service_center=self.coop.service_center
		self.notify_center=self.coop.notify_center
		self.server = self.coop.server

	
	#重启队列
	def command_restart(self,data):
		logging.info('coop stop')	
		log="coop stop\n"
		self.coop.stop()
		log+="coop start\n"
		self.coop.start()
		return log
		
	def create_command(self,mode,cmd,data):
		task = {'mode':mode,'command':cmd,'data':data}
		return json.dumps(task)
		
	#发送TCP消息
	def command_send_to(self,data):
		if 'data' in data:
			val=data['data']
		else:
			val = data
		self.server.sendTo(data['user'],val)
		return True

	#发送TCP消息
	def command_send_all(self,data):
		self.server.sendAll(data)
		return True
		
	#支持SQL类型的队列更新
	def command_sql(self,sql,data):
		logging.info('command_sql start')	
		self.sql_center.add(sql,data)
		return True
	
	
	#创建队列
	def command_create_queue(self,data):
		pass		